import torch
import matplotlib.pyplot as plt

x = torch.linspace(0, torch.pi / 2
                   , 20)
y = torch.sin(x)
plt.plot(x, y, 'rv-')


def tanh(x):
    a = torch.exp(x) - torch.exp(-x)
    b = torch.exp(x) + torch.exp(-x)
    return a / b


w = torch.tensor(0.1, requires_grad=True)
b = torch.tensor(0.2, requires_grad=True)
y_predict = tanh(x * w) + b
line, = plt.plot(x.detach().numpy(), y_predict.detach().numpy(), 'g^--')

epochs = 1000
for epoch in range(epochs):
    y_predict = tanh(x * w) + b
    line.set_data(x.detach().numpy(), y_predict.detach().numpy())
    e = torch.sum((y_predict - y) ** 2)
    e.backward()
    with torch.no_grad():
        w -= w.grad * 0.01
        b -= b.grad * 0.01
        w.grad.zero_()
        b.grad.zero_()
    print(e)
    plt.pause(0.1)
# plt.show()
